package top.lingkang.securityoauth2.client.config;

import top.lingkang.securityoauth2.client.base.CheckAuthorize;
import top.lingkang.securityoauth2.client.base.CheckPathCache;
import top.lingkang.securityoauth2.client.base.FinalExceptionHandler;
import top.lingkang.securityoauth2.core.AccessTokenHandle;
import top.lingkang.securityoauth2.client.base.impl.DefaultCheckPathCache;
import top.lingkang.securityoauth2.client.base.impl.DefaultFinalExceptionHandler;
import top.lingkang.securityoauth2.core.DefaultAccessTokenHandleImpl;
import top.lingkang.securityoauth2.client.http.FinalHttpRequest;
import top.lingkang.securityoauth2.client.http.impl.DefaultFinalHttpRequest;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author lingkang
 * Created by 2022/1/7
 */
public class Oauth2ClientProperties {
    // 排除路径
    private String[] excludePath = {};

    // 服务器地址
    private String[] serverURL = new String[]{"http://127.0.0.1:9512"};

    // 客户端基本配置
    private String clientId;
    private String clientSecret;
    private String resourceId;

    // 权限校验
    private CheckAuthorize checkAuthorize = new CheckAuthorize();

    private FinalExceptionHandler exceptionHandler = new DefaultFinalExceptionHandler();

    // 路径鉴权缓存
    private CheckPathCache checkPathCache = new DefaultCheckPathCache();

    private FinalHttpRequest httpRequest = new DefaultFinalHttpRequest();

    private AccessTokenHandle accessTokenHandle = new DefaultAccessTokenHandleImpl();


    // ----------------------------------------------------------------------


    public String getClientId() {
        return clientId;
    }

    public Oauth2ClientProperties setClientId(String clientId) {
        this.clientId = clientId;
        return this;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public Oauth2ClientProperties setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
        return this;
    }

    public String getResourceId() {
        return resourceId;
    }

    public Oauth2ClientProperties setResourceId(String resourceId) {
        this.resourceId = resourceId;
        return this;
    }

    public AccessTokenHandle getGetToken() {
        return accessTokenHandle;
    }

    /**
     * 配置token获取方式
     *
     * @param accessTokenHandle
     * @return
     */
    public Oauth2ClientProperties setGetToken(AccessTokenHandle accessTokenHandle) {
        this.accessTokenHandle = accessTokenHandle;
        return this;
    }

    public FinalHttpRequest getHttpRequest() {
        return httpRequest;
    }

    /**
     * 配置与服务端交互的get、post请求接口
     *
     * @param httpRequest
     * @return
     */
    public Oauth2ClientProperties setHttpRequest(FinalHttpRequest httpRequest) {
        this.httpRequest = httpRequest;
        return this;
    }

    public String[] getServerURL() {
        return serverURL;
    }

    public Oauth2ClientProperties setServerURL(String... serverURL) {
        this.serverURL = serverURL;
        return this;
    }

    public CheckPathCache getCheckPathCache() {
        return checkPathCache;
    }

    /**
     * 权限校验路径过滤缓存
     *
     * @param checkPathCache
     * @return
     */
    public Oauth2ClientProperties setCheckPathCache(CheckPathCache checkPathCache) {
        this.checkPathCache = checkPathCache;
        return this;
    }

    public FinalExceptionHandler getExceptionHandler() {
        return exceptionHandler;
    }

    public Oauth2ClientProperties setExceptionHandler(FinalExceptionHandler exceptionHandler) {
        this.exceptionHandler = exceptionHandler;
        return this;
    }

    public String[] getExcludePath() {
        return excludePath;
    }

    public Oauth2ClientProperties setExcludePath(String... excludePath) {
        Set<String> ex = new HashSet<>(Arrays.asList(excludePath));
        this.excludePath = ex.toArray(new String[ex.size()]);
        return this;
    }

    public CheckAuthorize checkAuthorize() {
        return checkAuthorize;
    }

    public CheckAuthorize getCheckAuthorize() {
        return checkAuthorize;
    }
}
